package com.example.rectanimationtest.AnimaUtils;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
/**
 * 使用实现动画的一些方法
 * @author zhengshaorui
 *
 */
public class IAnimationHelperImp implements IAnimationHelper{

	private static final int SCALE_ANIMATION_TIME = 500;
	private static final int MOVE_ANIMATION_TIME = 300;
	private static final float DEFUALT_SCALE = 1.0f;
	private MainUpView mainUpView;
	private AnimatorSet mCurrentAnimatorSet;


	/**
	 * 获取边框和阴影
	 * @param canvas
     */
	@Override
	public void MainUpViewDraw(Canvas canvas) {
		onDrawShadow(canvas);
		onDrawUpRect(canvas);
	}



	/**
	 * 绘制外部阴影.
	 */
	public void onDrawShadow(Canvas canvas) {
		Drawable drawableShadow = getMainUpView().getShadowDrawable();
		if (drawableShadow != null) {
			Rect shadowPaddingRect = getMainUpView().getDrawShadowRect();
			int width = getMainUpView().getWidth();
			int height = getMainUpView().getHeight();
			Rect padding = new Rect();
			drawableShadow.getPadding(padding);
			drawableShadow.setBounds(-padding.left + (shadowPaddingRect.left), -padding.top + (shadowPaddingRect.top),
					width + padding.right - (shadowPaddingRect.right),
					height + padding.bottom - (shadowPaddingRect.bottom));
			drawableShadow.draw(canvas);
		}
	}

	/**
	 * 绘制最上层的移动边框.
	 */
	public void onDrawUpRect(Canvas canvas) {
		Drawable drawableUp = getMainUpView().getUpRectDrawable();
		if (drawableUp != null) {
			Rect paddingRect = getMainUpView().getDrawUpRect();
			int width = getMainUpView().getWidth();
			int height = getMainUpView().getHeight();
			Rect padding = new Rect();
			// 边框的绘制.
			drawableUp.getPadding(padding);
			drawableUp.setBounds(-padding.left + (paddingRect.left), -padding.top + (paddingRect.top),
					width + padding.right - (paddingRect.right), height + padding.bottom - (paddingRect.bottom));
			drawableUp.draw(canvas);
		}
	}

	@Override
	public void setFocusView(View currentView, View oldView, float scale) {
		// TODO Auto-generated method stub
		int getScale = (int)(scale * 10);
		if (getScale > 10) {
			setViewScale(currentView,oldView,scale);
		}
		rectMoveAnimation(currentView, scale, scale);
	}
	
	private void setViewScale(View currentView, View oldView, float scale){
		if (currentView != null) {
			currentView.animate().scaleX(scale).scaleY(scale).setDuration(SCALE_ANIMATION_TIME).start();
			if (oldView != null) {
				oldView.animate().scaleX(DEFUALT_SCALE).scaleY(DEFUALT_SCALE).setDuration(MOVE_ANIMATION_TIME).start();
			}

		}
	}

	private Rect findLocationWithView(View view){
		ViewGroup root = (ViewGroup) getMainUpView().getParent();
		Rect rect = new Rect();
		root.offsetDescendantRectToMyCoords(view,rect);
		return rect;
	}
	public void rectMoveAnimation(View focusView,float scaleX,float scaleY){
		Rect fromRect = findLocationWithView(getMainUpView());
		Rect toRect = findLocationWithView(focusView);
		int disX = toRect.left - fromRect.left;
		int disY = toRect.top - fromRect.top;
		rectMoveMainLogic(focusView,disX,disY,scaleX,scaleY);

	}



	private void rectMoveMainLogic(View focusView, float x, float y,float scaleX,float scaleY)
	{
		int newWidth = 0;
		int newHeight = 0;
		int oldWidth = 0;
		int oldHeight = 0;
		if (focusView != null) {
			newWidth = (int) (focusView.getMeasuredWidth() * scaleX);
			newHeight = (int) (focusView.getMeasuredHeight() * scaleY);
			x = x + (focusView.getMeasuredWidth() - newWidth) / 2;
			y = y + (focusView.getMeasuredHeight() - newHeight) / 2;
		}

		// 取消之前的动画.
		if (mCurrentAnimatorSet != null)
			mCurrentAnimatorSet.cancel();

		oldWidth = getMainUpView().getMeasuredWidth();
		oldHeight = getMainUpView().getMeasuredHeight();

		ObjectAnimator transAnimatorX = ObjectAnimator.ofFloat(getMainUpView(), "translationX", x);
		ObjectAnimator transAnimatorY = ObjectAnimator.ofFloat(getMainUpView(), "translationY", y);
		ObjectAnimator scaleXAnimator = ObjectAnimator.ofInt(new ScaleView(getMainUpView()), "width", oldWidth,
				(int) newWidth);
		ObjectAnimator scaleYAnimator = ObjectAnimator.ofInt(new ScaleView(getMainUpView()), "height", oldHeight,
				(int) newHeight);
		//
		AnimatorSet mAnimatorSet = new AnimatorSet();
		mAnimatorSet.playTogether(transAnimatorX, transAnimatorY, scaleXAnimator, scaleYAnimator);
		mAnimatorSet.setInterpolator(new DecelerateInterpolator(1));
		mAnimatorSet.setDuration(MOVE_ANIMATION_TIME);
		getMainUpView().setVisibility(View.VISIBLE);
		mAnimatorSet.start();
		mCurrentAnimatorSet = mAnimatorSet;

	}
	
	
	/**
	 * 用於放大的view
	 */
	public class ScaleView {
		private View view;
		private int width;
		private int height;

		public ScaleView(View view) {
			this.view = view;
		}

		public int getWidth() {
			return view.getLayoutParams().width;
		}

		public void setWidth(int width) {
			this.width = width;
			view.getLayoutParams().width = width;
			view.requestLayout();  //重新刷新位置
		}

		public int getHeight() {
			return view.getLayoutParams().height;
			
		}

		public void setHeight(int height) {
			this.height = height;
			view.getLayoutParams().height = height;
			view.requestLayout();
		}
	}

	@Override
	public MainUpView getMainUpView() {
		// TODO Auto-generated method stub
		return this.mainUpView;
	}

	@Override
	public void setMainUpView(MainUpView mainView) {
		// TODO Auto-generated method stub
		this.mainUpView = mainView;
	}
	
	
	

}
